home *** CD-ROM | disk | FTP | other *** search
- ' The PointXY class demonstrates how an object can override the ToString to
- ' provide a textual representation of its current state.
-
- Class PointXY
- Public X, Y As Double
-
- Sub New(ByVal X As Double, ByVal Y As Double)
- Me.X = X
- Me.Y = Y
- End Sub
-
- ' Return the X-Y coordinates in the format "(X,Y)"
- Overrides Function ToString() As String
- Return "(" & X.ToString & "," & Y.ToString & ")"
- End Function
-
- End Class
-
- ' a class that implements IFormattable interface
-
- Class PointXY2
- Implements IFormattable
-
- Public X, Y As Double
-
- Sub New(ByVal X As Double, ByVal Y As Double)
- Me.X = X
- Me.Y = Y
- End Sub
-
- ' Return the X-Y coordinates in the format "(X,Y)"
- Overrides Function ToString() As String
- Return "(" & X.ToString & "," & Y.ToString & ")"
- End Function
-
- Private Function Format(ByVal FormatStr As String, _
- ByVal fp As IFormatProvider) As String _
- Implements IFormattable.ToString
-
- If FormatStr = "" Then
- ' If no formatting is passed, use default formatting
- Return Me.ToString
- Else
- ' Otherwise replace X and Y characters with actual coordinates.
- Return Replace(Replace(FormatStr, "X", X.ToString), _
- "Y", Y.ToString)
- End If
- End Function
-
- End Class
-
- ' The MetricConverter class demonstrates the IFormatProvider and ICustomFormatter interfaces
-
- Class MetricConverter
- Implements IFormatProvider
- Implements ICustomFormatter
-
- ' The only method of the IFormatProvider interface
- Private Function GetFormat(ByVal Service As System.Type) _
- As Object Implements IFormatProvider.GetFormat
-
- If Service.Name = "ICustomFormatter" Then
- ' Return this instance if called from the String.Format method.
- Return Me
- Else
- ' Return Nothing in all other cases.
- Return Nothing
- End If
- End Function
-
- ' The only method of the ICustomFormatter interface
- Function Format(ByVal formatStr As String, ByVal arg As Object, _
- ByVal fp As IFormatProvider) As String _
- Implements ICustomFormatter.Format
-
- If formatStr = "" Then
- ' No format string, return the argument.
- Return arg.ToString
- ElseIf formatStr = "in" Then
- ' Convert inches to meters.
- Return (CDbl(arg) * 0.0254).ToString
- ElseIf formatStr = "fe" Then
- ' Convert feet to meters.
- Return (CDbl(arg) * 0.33).ToString
- End If
- End Function
- End Class
-
- ' two Enums for our experiments
-
- ' This Enum defines the data type accepted for a
- ' value entered by the end user.
- Enum DataEntry As Short
- IntegerNumber
- FloatingNumber
- CharString
- DateTime
- End Enum
-
- <Flags()> _
- Enum ValidDataEntry As Short
- None = 0 ' Always define an enum value = 0.
- IntegerNumber = 1
- FloatingNumber = 2
- CharString = 4
- DateTime = 8
- End Enum
-